<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Request;
use yii\web\Response;
use proxy\HttpProxy;
use helper\SessionHelper;
use yii\rbac\Item;
use app\models\MenuSearch;

class BasedataController extends SecBaseController
{
    public function actionVender_list(){
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        
        try{ 
            $ret = $this->proxy->execute('basedata', 'vender_list', $this->remote_params);
        }catch(\Exception $e){
            $this->return_page_values['errno'] = $e->getCode();
            $this->return_page_values['errmsg'] = $e->getMessage();
            return $this->myRenderFile('@tpl/page/error.tpl', $this->return_page_values);
        }

        return $ret;

    }

    public function actionGoods_list(){
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;

        $goods_type = $request->get("goods_type"); 
        $vender_id = $request->get("vender_id"); 

        $this->remote_params['goods_type'] = $goods_type ; 
        $this->remote_params['vender_id'] = $vender_id ; 
        
        try{ 
            $ret = $this->proxy->execute('basedata', 'goods_list', $this->remote_params);
        }catch(\Exception $e){
            $this->return_page_values['errno'] = $e->getCode();
            $this->return_page_values['errmsg'] = $e->getMessage();
            return $this->myRenderFile('@tpl/page/error.tpl', $this->return_page_values);
        }

        return $ret;

    }

    //获取所有rbac的权限或角色
    public function actionRbacitems() {
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;
        
        $request = Yii::$app->request;
        $type = $request->get("type");

        $authManager = Yii::$app->authManager;
        $items = array();
        if (empty($type)) {
            $roles = $authManager->getRoles();
            $permissions = $authManager->getPermissions();
            $items = array_merge($roles, $items);
            $items = array_merge($permissions, $items);
        } else {
            if ($type == Item::TYPE_ROLE) {
                $items = $authManager->getRoles();
            }

            if ($type == Item::TYPE_PERMISSION) {
                $items = $authManager->getPermissions();
            }

        }

        $ret_array = array();
        foreach($items as $item) {
            $ret_item = array();
            $ret_item['id'] = $item->name; //rbac auth item use 'name' as id in table
            $ret_item['name'] = $item->name;
            $ret_array[] = $ret_item;
        }
    
        //$fake_data = array("id" => 1, "name" => "假数据");
        //$ret_array[] = $fake_data;
      
        $ret = array("data_list" => $ret_array);//conform to java format 
        return $ret;
    }

    //获取所有菜单
    public function actionAdminmenus() {
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;

        $menuSearch = new MenuSearch();
        $dataProvider = $menuSearch->search([]);
        $menus = $dataProvider->getModels();
        $menu_data_list = array();
        foreach($menus as $menu) {
            $menu_item = array();
            $menu_item['id'] = $menu->menu_id;
            $menu_item['name'] = $menu->name;
            $menu_data_list[] = $menu_item;
        }
        $ret = array("data_list" => $menu_data_list);
        return $ret;
    } 
}
